/*
 * @Author: songxiang songxiang0806@qq.com
 * @Date: 2025-08-28 15:04:11
 * @LastEditors: songxiang songxiang0806@qq.com
 * @LastEditTime: 2025-08-28 15:05:22
 * @FilePath: \supermap-webgl\src\utils\color-util.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const hexToRgb = (hex) => {
  hex = hex.replace('#', '')
  if (hex.length === 3) {
    hex = hex
      .split('')
      .map((c) => c + c)
      .join('')
  }
  const bigint = parseInt(hex, 16)
  return {
    r: (bigint >> 16) & 255,
    g: (bigint >> 8) & 255,
    b: bigint & 255
  }
}

const rgbToHex = (r, g, b) => {
  return (
    '#' +
    [r, g, b]
      .map((x) => {
        const hex = x.toString(16)
        return hex.length === 1 ? '0' + hex : hex
      })
      .join('')
  )
}

const getGradientColors = (minColor, maxColor, steps) => {
  const start = hexToRgb(minColor)
  const end = hexToRgb(maxColor)
  const colors = []

  for (let i = 0; i < steps; i++) {
    const r = Math.round(start.r + ((end.r - start.r) * i) / (steps - 1))
    const g = Math.round(start.g + ((end.g - start.g) * i) / (steps - 1))
    const b = Math.round(start.b + ((end.b - start.b) * i) / (steps - 1))
    colors.push(rgbToHex(r, g, b))
  }

  return colors
}

export { getGradientColors }
